//
//  APTabBarViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/1.
//

#import "APTabBarViewController.h"
#import "APHomeViewController.h"
#import "APVideosViewController.h"
#import "APDiscoveryViewController.h"
#import "APMeViewController.h"
#import "APBaseNavigationController.h"
#import "APLoginManager.h"
#import "AppDelegate.h"
#import "APLoginViewController.h"
#import "APUserDataManager.h"
#import "APVideoDataManager.h"
#import "APDiscoveryDataManager.h"

@interface APTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation APTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTabBar];
    [BaseDataManager loadAppConfig];
    [self.view ShowProgress];
    [[APUserDataManager shareInstance] loadUsers:^(NSArray * _Nonnull users) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [[APVideoDataManager shareInstance]  initVideoData:^(NSArray * _Nonnull videos) {}];
            [[APDiscoveryDataManager shareInstance]  loadData:^{}];
        });
        [self.view removeProgress];
    }];
}


- (void)initTabBar{
    self.delegate = self;
    self.tabBar.backgroundColor = UIColor.blackColor;
    self.tabBar.translucent = false;
    //主页
    UIViewController *homeVC = [self makeTabBarItem:@"" imageName:@"tab_home_n" heightlightImageName:@"tab_home_h" controller:[[APHomeViewController alloc] init]];
    
    UIViewController *videoVC = [self makeTabBarItem:@"" imageName:@"tab_video_n" heightlightImageName:@"tab_video_h" controller:[[APVideosViewController alloc] init]];
    
    UIViewController *discoveryVC = [self makeTabBarItem:@"" imageName:@"tab_discovery_n" heightlightImageName:@"tab_discovery_h" controller:[[APDiscoveryViewController alloc] init]];
    
    UIViewController *meVC = [self makeTabBarItem:@"" imageName:@"tab_me_n" heightlightImageName:@"tab_me_h" controller:[[APMeViewController alloc] init]];
    self.viewControllers = [[NSArray alloc] initWithObjects:homeVC,videoVC,discoveryVC,meVC, nil];
}


- (UIViewController *)makeTabBarItem:(NSString *)title imageName:(NSString *)imageName heightlightImageName:(NSString *)heightlightImageName controller:(UIViewController *)controller{
    APBaseNavigationController *navi  = [[APBaseNavigationController alloc] initWithRootViewController:controller];
    navi.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  selectedImage:[[UIImage imageNamed:heightlightImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return navi;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        
    if (tabBarController.selectedIndex == 0) {
        [APLogManager statisticWithPage:APPage_tab1];

    }else if (tabBarController.selectedIndex == 1){
        [APLogManager statisticWithPage:APPage_tab2];

    }else if (tabBarController.selectedIndex == 2){
        [APLogManager statisticWithPage:APPage_tab3];
    }else{
        [APLogManager statisticWithPage:APPage_tab4];

    }
    
    
}


@end
